import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/box/conteroller/box_details_controller.dart';
import 'package:nft_shop/box/model/models.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/widgets/load_image.dart';

enum BoxInfoType {
  writer,//作者
  describe,//描述
  detail,//细节
  story,//故事
}

class BoxInfoWidget extends StatefulWidget {
  BoxInfoType type;
  BoxInfoWidget({Key? key,required this.type}) : super(key: key);

  @override
  _BoxInfoWidgetState createState() => _BoxInfoWidgetState();
}

class _BoxInfoWidgetState extends State<BoxInfoWidget> {
  late BoxDetailsConteroller _conteroller;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _conteroller = BoxDetailsConteroller.to;
  }

  @override
  Widget build(BuildContext context) {
    if(widget.type == BoxInfoType.writer){
      return _framerWidget();
    }else if(widget.type == BoxInfoType.describe){
      return _worksClassification();
    }else if(widget.type == BoxInfoType.story){
      return _worksDescribeWidget();
    }else{
      return _detailsWidget();
    }
    return Container();
  }

  ///创作者介绍
  Widget _framerWidget(){
    return Container(
      padding: EdgeInsets.only(top: 7),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              ClipRRect(borderRadius: BorderRadius.circular(21),child: LoadImage(_conteroller.boxInfo.info!.portrait??"header_img",width: 42,height: 42,)),
              SizedBox(width: 5,),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(_conteroller.boxInfo.info!.nickname??"",maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 14,color: Global.kTheme.textPrimary0,fontFamily: Global.mySemiboldFont),),
                    InkWell(
                      onTap: (){
                        ClipboardData data = new ClipboardData(
                            text: _conteroller.boxInfo.info!.framerAccountAddress??"");
                        Clipboard.setData(data);
                        EasyLoading.showToast("复制成功");
                      },
                      child: Row(
                        children: [
                          Text(omitAddress(_conteroller.boxInfo.info!.framerAccountAddress??"")),
                          SizedBox(width: 4,),
                          LoadImage("my_copy",width: 16,height: 16,),
                        ],
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
          SizedBox(height: 10,),
          Text(_conteroller.boxInfo.info!.introduce??"",
            style: TextStyle(fontSize: 12,color: Global.kTheme.inputHint1,fontFamily: Global.mySemiboldFont),
          )
        ],
      ),
    );
  }

  ///作品描述
  Widget _worksClassification(){
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            height: 40,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
              Text("您可能抽到以下作品中的",style: TextStyle(fontSize: 12,color: Global.kTheme.inputHint1,fontFamily: Global.mySemiboldFont),),
              Text("1",style: TextStyle(fontSize: 12,color: Global.kTheme.textPrimary4,fontFamily: Global.mySemiboldFont),),
              Text("个",style: TextStyle(fontSize: 12,color: Global.kTheme.inputHint1,fontFamily: Global.mySemiboldFont),),
            ],),
          ),
          Container(
            height: _conteroller.boxInfo.specialBlindBoxList!.length * 70,
            child: ListView.builder(
              itemCount:_conteroller.boxInfo.specialBlindBoxList!.length,
              physics: const NeverScrollableScrollPhysics(),
              itemBuilder: (_, index) {
                return _worksItem(index);
              },
            ),
          ),
        ],
      )
    );
  }

  Widget _worksItem(int index){
    SpecialBlindBoxList model = _conteroller.boxInfo.specialBlindBoxList![index];
    return Container(
      height: 60,
      margin: EdgeInsets.symmetric(vertical: 5),
      padding: EdgeInsets.symmetric(horizontal: 15,vertical: 10),
      decoration: BoxDecoration(
        color: Color(0xFFF8F8F8),
        borderRadius: BorderRadius.circular(4),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(model.title??"",style: TextStyle(fontSize:12,color: Global.kTheme.textPrimary1,fontFamily: Global.mySemiboldFont),),
          SizedBox(height: 5,),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
            Text.rich(TextSpan(children: [
              TextSpan(text: "概率 ",style: TextStyle(fontSize: 12,color: Global.kTheme.textPrimary3,fontFamily: Global.mySemiboldFont)),
              TextSpan(text: "${NumUtil.multiply(double.parse(model.probability??"0"),100).toStringAsFixed(2) }%",style: TextStyle(fontSize: 12,color: Global.kTheme.textPrimary4,fontFamily: Global.mySemiboldFont)),
            ])),
            Text.rich(TextSpan(children: [
              TextSpan(text: "原价 ",style: TextStyle(fontSize: 12,color: Global.kTheme.textPrimary3,fontFamily: Global.mySemiboldFont)),
              TextSpan(text: "¥ ${model.basePrice}",style: TextStyle(fontSize: 12,color: Global.kTheme.textPrimary4,fontFamily: Global.mySemiboldFont)),
            ])),
          ],)
        ],
      ),
    );
  }

  //细节展示
  Widget _detailsWidget(){
    return LoadImage(_conteroller.boxInfo.info!.detail??"",width: Get.width,fit: BoxFit.fitWidth,);
  }


  ///作品描述
  Widget _worksDescribeWidget(){
    return Container(
      padding: EdgeInsets.only(top: 7),
      child: Text(_conteroller.boxInfo.info!.story??"",
        style: TextStyle(fontSize: 12,color: Global.kTheme.inputHint1,fontFamily: Global.mySemiboldFont),
      ),
    );
  }

  String omitAddress(String address, {int len = 4}) {
    if (address == null || address.isEmpty) return "";
    if (address.length > (len * 2)) {
      return address.substring(0, len) + '...' + address.substring(address.length - len, address.length);
    }
    return address;
  }

}
